//
//  ProductViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/11/16.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "ProductViewController.h"
#import "JHCustomMenu.h"
//#import "TempletViewController.h"

#import "Templet2ViewController.h"

#import "ManualViewController.h"
#import "Manual2ViewController.h"
#import "AFNetworking.h"


#import "MenuItemModel.h"
#import "CellModel.h"
#import "ProductViewCell.h"


//编辑预览
#import "EditViewController.h"
#import "PreViewController.h"


//编辑页面
#import "EditView2Controller.h"
#import "MBProgressHUD+MJ.h"

#import "LYButton.h"



#define TABLEVIEW_CELL_REUSE_ID @"TABLEVIEW_CELL_REUSE_ID"



@interface ProductViewController ()<JHCustomMenuDelegate,MenutableViewCellDataSource,MenuTableViewCellDelegate> {
    
    NSIndexPath *deletIndexPath;
    NSString *productIDDef;
    BOOL isRow;
    BOOL isAllChoose;
    NSInteger deleteInteger;
    NSMutableArray *dataArray;
    UIButton *proBtn;
    ProductViewCell *cell;
    
    UIView *viewAdd;
    BOOL isGuanli;
    UIButton *btnRight2;

    
    
}



@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong) UIButton *delegateBtn;
//已经打开下拉菜单的单元格
@property (strong, nonatomic) ProductViewCell *openedMenuCell;
//已经打开下拉菜单的单元格的位置
@property (strong, nonatomic) NSIndexPath *openedMenuCellIndex;
//下拉菜单数据源
@property (strong, nonatomic) NSMutableArray *menuItemDataSourceArray;
//设置边框的menu
@property(nonatomic,strong) JHCustomMenu *menu;
//获取网络信息的数组
@property(nonatomic,strong) NSMutableArray *list;
//网路请求
@property(nonatomic,strong)AFHTTPSessionManager *manager;
//重新获得新的数组,单选多选的数据进行重新组合
@property(nonatomic,strong) NSMutableArray *dataArray;
//单选与多项标示
//设定bool标示
@property(nonatomic,strong) NSMutableArray *boolArray;
//进行删除第二个操作商品ID
@property(nonatomic,strong) NSString *productID;
//无订单显示图片view加image以及button
@property(nonatomic,strong) UIView *view20w;
@property(nonatomic,strong) UIButton *btnw;
@property(nonatomic,strong) UIImageView *imageVieww;
//
////全选出现的view
//@property(nonatomic,strong) UIView *viewAdd;

@end

@implementation ProductViewController


//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}


#pragma mark tab显示或者隐藏处理

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    self.tabBarController.tabBar.hidden = NO;
    self.tabBarController.tabBar.alpha = 1;
    
    

}




//移除观察者
-(void)dealloc
{


    [[NSNotificationCenter defaultCenter] removeObserver:self];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = [NSMutableArray arrayWithCapacity:0];
    self.dataArray = [[NSMutableArray alloc]initWithCapacity:0];

    self.view.backgroundColor = BACKGROUND1;
    
    self.tableView.backgroundColor = BACKGROUND1;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    //添加观察者进行这个页面的刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAPI11) name:@"shangchuan" object:nil];
    
    
    
    //给修改信息添加观察者进行页面信息刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAPI11) name:@"xiugai" object:nil];
    
    
    
    //模板导入进行页面刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAPI11) name:@"muban" object:nil];
    
    
    self.titleLabel.text = @"商品列表";
    self.rightBut.hidden = NO;
    self.rightBut.frame = CGRectMake(ViewWidth *0.81, 18, 70, 44);
    [self.rightBut setImage:[UIImage imageNamed:@"add1"] forState:UIControlStateNormal];
    btnRight2 = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth *0.81, 18, 70, 44)];
    [btnRight2 setTitle:@"取消" forState:UIControlStateNormal];
    btnRight2.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [self.topView addSubview:btnRight2];
    btnRight2.hidden = YES;
    [btnRight2 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.backBtn.hidden = NO;
    [self.backBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    self.backBtn.frame = CGRectMake(12, 20, 44, 44);
    self.backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    [self initBottom];
    
    self.view20w = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight-64-64)];
    
    self.view20w.backgroundColor= BACKGROUND1;
    [self.view addSubview:self.view20w];
    
    self.imageVieww = [[UIImageView alloc]initWithFrame:CGRectMake((self.view20w.frame.size.width-80)/2, (self.view20w.frame.size.height-110-70)/2, 80, 100)];
    self.imageVieww.image = [UIImage imageNamed:@"null_default"];
//    null_default
    
    [self.view20w addSubview:self.imageVieww];
    
    
    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,(self.view20w.frame.size.height-80-70)/2+self.imageVieww.frame.size.height+10, self.view20w.frame.size.width, 40)];
    lbl.text = @"  还没有新商品哦~";
    lbl.textColor = [UIColor grayColor];
    lbl.textAlignment  = NSTextAlignmentCenter;
    lbl.font = [UIFont systemFontOfSize:18];
    [self.view20w addSubview:lbl];
    
    self.btnw  = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight-48-64-46)];
    [self.btnw addTarget:self action:@selector(sendAPI11) forControlEvents:UIControlEventTouchUpInside];
    [self.view20w addSubview:self.btnw];
    self.view20w.hidden = YES;
    self.tableView.hidden = NO;
    self.tableView.allowsSelectionDuringEditing = YES;

//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //制造下拉菜单的按钮
    [self buildDataSource];
    
    [self sendAPI11];

}

#pragma mark 设置底部bottom
-(void)initBottom

{
    
//    proBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,ViewHeight -44 , 90, 44)];
//    if (!isAllChoose) {
//        
//        [proBtn setTitle:@"全  选" forState:UIControlStateNormal];
//    } else {
//        
//        [proBtn setTitle:@"取  消" forState:UIControlStateNormal];
//    
//    }
    
//    proBtn.titleLabel.font = [UIFont systemFontOfSize:18];
//    [proBtn setTitleColor:BenSe_COLOR forState:UIControlStateNormal];
//    [proBtn addTarget:self action:@selector(allChooseClick) forControlEvents:UIControlEventTouchUpInside];
//    [proBtn setBackgroundColor:BACKGROUND1];
//    [self.view addSubview:proBtn];
//    
//    self.delegateBtn.backgroundColor = BenSe_COLOR;
//    [self.delegateBtn addTarget:self action:@selector(delete2:) forControlEvents:UIControlEventTouchUpInside];
//    self.delegateBtn.backgroundColor = BenSe_COLOR;
//    [self.delegateBtn setTitle:@"删        除" forState:UIControlStateNormal];
//    self.delegateBtn.titleLabel.font = [UIFont systemFontOfSize:20];
//    [self.delegateBtn setTintColor:[UIColor whiteColor]];
//    [self.delegateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    self.delegateBtn.layer.cornerRadius = 3.0;
//    
    
    
  
    
    
    
    
    
    
}

- (void)allChooseClick {
    
    if (self.list.count == 0) {
        
        [self.view makeToast:@"暂无商品"];
        return;
    }
    
    [self.dataArray removeAllObjects];
    if (!isAllChoose) {
        
        for (int i = 0; i < self.list.count; i ++) {
            
            self.boolArray[i] = @"YES";
            [self.dataArray addObject:self.list[i]];
        }
        isAllChoose = YES;
        [proBtn setTitle:@"取  消" forState:UIControlStateNormal];
    } else {
        
        for (int i = 0; i < self.list.count; i ++) {
            
            self.boolArray[i] = @"NO";
        }
        isAllChoose = NO;
        [proBtn setTitle:@"全  选" forState:UIControlStateNormal];
    }
    
    [self.tableView reloadData];
}

#pragma mark 重新构建leftBtn的方法

- (void)back:(UIButton *)sender {
    
    
    

//    if (self.tabBarController.tabBar.hidden == NO) {
//        self.tabBarController.tabBar.hidden = YES;
//        [self.backBtn setTitle:@"取消" forState:UIControlStateNormal];
//        self.tableView.editing = YES;
//        self.rightBut.enabled = NO;
//        isGuanli = YES;
//        
//
//    }
//    else
//    {
//        self.tabBarController.tabBar.hidden = NO;
//    
//        [self.backBtn setTitle:@"管理" forState:UIControlStateNormal];
//        self.tableView.editing = NO;
//        self.rightBut.enabled = YES;
//        isGuanli = NO;
//    }
    
    if (!isGuanli) {
        
        [self.backBtn setTitle:@"取消" forState:UIControlStateNormal];
         self.tableView.editing = YES;
         self.rightBut.enabled = NO;
        isGuanli = YES;
        self.rightBut.hidden = YES;
        btnRight2.hidden = NO;
        self.backBtn.hidden = YES;
        
    }
    else
    {
        [self.backBtn setTitle:@"编辑" forState:UIControlStateNormal];
        self.tableView.editing = NO;
        self.rightBut.enabled = YES;
        isGuanli = NO;
        self.rightBut.hidden = NO;
        btnRight2.hidden = YES;
        self.backBtn.hidden = NO;

    }
    
    if (!isAllChoose) {
        
        [proBtn setTitle:@"全  选" forState:UIControlStateNormal];
    } else {
        
        [proBtn setTitle:@"取  消" forState:UIControlStateNormal];
    }
    
    [self.tableView reloadData];
    
    
    
}


#pragma mark 不出现delegate的操作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return UITableViewCellEditingStyleNone;
    
    
}


#pragma mark删除的操作方法

-(void)delete2:(UIButton *) sender
{
    self.productID = @"";
    int i ;
    for ( i = 0; i < self.boolArray.count; i++) {
        
        NSString *boolStr = self.boolArray[i];
        if ([boolStr isEqualToString:@"YES"]) {
            
            NSDictionary *dic = [self.list objectAtIndex:i];
            NSString *ID = [dic valueForKey:@"product_id"];
            if (!i) {
                
                {
                
                
                    self.productID = [NSString stringWithFormat:@"%@",ID];
                
                }
                
            }
            
            else
            {
            
                self.productID = [NSString stringWithFormat:@"%@,%@",self.productID,ID];
            
            }
            
            
        }
        
        
    }
    
    //如果没有商品被选择删除提示没有商品
    
    
    if ([self.productID isEqualToString:@""]) {
        
        
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请选择商品" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alter show];
        return;
    }
    
    
    [self.tableView reloadData];


    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  message:@"确定要删除商品吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    [alert show];


}



//#pragma mark 进行删除操作
//
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    
    
    if (buttonIndex==1) {
        
        //        如果删除完了，就显出没商品的图片
//        if (self.list.count==0) {
//            
//            self.tableView.hidden = YES;
//            self.view20w.hidden = NO;
////            [self.tableView reloadData];
//        }
        
        if (isRow) {
            
            isRow = NO;
            
            //从网络删除数据
            NSString *urlStr = [NSString stringWithFormat:@"%@delete_product",SERVER_ADDRESS2];
            
            NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"product_id" : productIDDef};
            
            [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
                if ([responseObject[@"error_flag"] intValue] == 0) {
                    if (![responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
                        
                        return ;
                    } else {
                        
//                        NSLog(@"00000000 %@  删除成功了",responseObject);
                    }
                } else {
                    
                    NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
                    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    [alter show];
                }
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接网络失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alter show];
                
            }];
            
          
            //刷新数据源
            [self.list removeObjectAtIndex:deleteInteger];
            //刷新列表
            [self.tableView deleteRowsAtIndexPaths:@[deletIndexPath] withRowAnimation:UITableViewRowAnimationMiddle];
            
            if (self.list.count==0) {
                
                self.tableView.hidden = YES;
                self.view20w.hidden = NO;
            }

            
            //如果不执行下面的代码cell的moreBtn的tag就没办法刷新。
            [self.tableView reloadData];
            
            
            

            
        }else {
        
        
        NSArray *array = [NSArray arrayWithArray:self.list];
        for  (NSDictionary *dict in self.dataArray)
        {
            for  (NSDictionary *dict0 in array)
            {
                if ([[dict objectForKey:@"product_id"] isEqualToString:[dict0 objectForKey:@"product_id"]]) {
                    [self.list removeObject:dict0];
                }
                //将YES的数据移除
                NSArray *array1 = [NSArray arrayWithArray:self.boolArray];
                for (NSString *str in array1) {
                    if ([str isEqualToString:@"YES"]) {
                        [self.boolArray removeObject:str];
                    }
                }
//                [self.tableView reloadData];
            }
        }
     
       //上传productID到服务器，进行删除
        NSString *urlStr = [NSString stringWithFormat:@"%@delete_product",SERVER_ADDRESS2];
        
        NSDictionary *params =@{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"product_id" : self.productID};
        [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([responseObject[@"error_flag"] intValue] == 0) {
                
//                NSLog(@"删除成功2");
                
                if (isAllChoose) {
                    
                    isAllChoose = NO;
                    [proBtn setTitle:@"全  选" forState:UIControlStateNormal];
                    self.tableView.hidden = YES;
                    self.view20w.hidden = NO;
                }
                
                [self.tableView reloadData];
            } else {
                
                NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
                
                UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alter show];
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
        }];
        }
    } else {
    
    }
}



/**
 *  @author li_yong
 *
 *  构建数据源
 */
- (void)buildDataSource


{
    //构建单元格下拉列表数据源
    self.menuItemDataSourceArray = [NSMutableArray arrayWithCapacity:0];
    MenuItemModel *favItemModel = [[MenuItemModel alloc] initWithNormalImageName:@"product_edit2"
                                                        withHighLightedImageName:@""
                                                                    withItemText:@"编辑"];
    [self.menuItemDataSourceArray addObject:favItemModel];
    
    MenuItemModel *albItemModel = [[MenuItemModel alloc] initWithNormalImageName:@"iconfont-yulan3"
                                                        withHighLightedImageName:@""
    
                                                                    withItemText:@"预览"];
    
    [self.menuItemDataSourceArray addObject:albItemModel];
    
    
    
    
//    LYButton *btn = [[LYButton alloc]init];
//     btn.buttonImageView.bounds = CGRectMake(0, 0, 30, 16);
//    btn.buttonImageView.center = CGPointMake(btn.frame.size.width/2, 20) ;
//    btn.buttonImageView.backgroundColor = [UIColor greenColor];


    
    
    
    
    
    MenuItemModel *dldItemModel = [[MenuItemModel alloc] initWithNormalImageName:@"product_delete2"
                                                        withHighLightedImageName:@""
                                                                    withItemText:@"删除"];
    [self.menuItemDataSourceArray addObject:dldItemModel];

    
    
    
   
}










#pragma mark获取网络信息
-(void)sendAPI11
{
   
    
    NSString *urlStr = [NSString stringWithFormat:@"%@product_list",SERVER_ADDRESS2];
     NSDictionary *params = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken};
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    [self.manager POST:urlStr parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"error_flag"] intValue] == 0)

        {
            
           
        
            if(![responseObject[@"data"] isKindOfClass:[NSDictionary class]] )
            {
                
                [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
                return ;
                
            }
            else
            {
                
                NSArray *array = responseObject[@"data"][@"product_list"];
                
                
                if (array.count != 0 )
                {
                    
                    
                    [self.list removeAllObjects];
                    
                    
                    self.list = [NSMutableArray arrayWithArray:array];
                    
                    self.view20w.hidden = YES;
                    self.tableView.hidden = NO;
                    
                }
                else
                {
                
                
                    self.view20w.hidden = NO;
                    self.tableView.hidden = YES;
                
                }

               


            }

        
            self.boolArray = [[NSMutableArray alloc]initWithCapacity:0];
            for (int i = 0; i < self.list.count; i++) {
                
                [self.boolArray addObject:@"NO"];
            }
            
            
            [self.tableView reloadData];
              [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        
        
        }
        
        else
        {
            self.view20w.hidden = NO;
            self.tableView.hidden = YES;
        
              [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
        
        
        
        
        }
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        self.view20w.hidden = NO;
        self.tableView.hidden = YES;
        
          [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请检查网络!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];
    
    
    
    
}




#pragma 侧边栏menu的显示
//点击出现menu的控件设置
- (void)rightButClick:(id)sender {
    
    __weak __typeof(self) weakSelf = self;
    if (!self.menu) {
        self.menu = [[JHCustomMenu alloc] initWithDataArr:@[@"模板导入", @"手动添加"] origin:CGPointMake(0, 0) width:130 rowHeight:44];
        
        _menu.delegate = self;
        
        
        
        _menu.arrImgName = @[@"", @""];
        [self.view addSubview:_menu];
    } else {
        [_menu dismissWithCompletion:^(JHCustomMenu *object) {
            weakSelf.menu = nil;
        }];
    }
}


- (void)jhCustomMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"select: %ld", indexPath.row);
    
    if (indexPath.row == 0) {
        

    
        Templet2ViewController *temp = [[Templet2ViewController alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:temp animated:YES];
      
    }
    else
    {
    
        
        Manual2ViewController *manual2 = [[Manual2ViewController alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:manual2 animated:YES];
    
    
    }
    
    
    __weak __typeof(self) weakSelf = self;

    _menu.dismiss = ^() {
        weakSelf.menu = nil;
    };
    
}


#pragma mark  tableViewDelegate以及dataSource的代理实现




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return self.list.count;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (!isGuanli) {
       return 0.001;
        
    }
    else
    {
    
        return 44+15;
    
    }

}




-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (!isGuanli) {
        
        return nil;
        
    }
    else
    {
        
        
        UIView *View2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 44+15)];
        View2.backgroundColor = BACKGROUND1;
        [self.view addSubview:View2];
        
        viewAdd = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, 44)];
        viewAdd.backgroundColor = [UIColor whiteColor];
        [View2 addSubview:viewAdd];
        
        
        proBtn = [[UIButton alloc]initWithFrame:CGRectMake(0,0 , 100, 44)];
        if (!isAllChoose) {
            
            [proBtn setTitle:@"全选" forState:UIControlStateNormal];
            [proBtn setImage:[UIImage imageNamed:@"gou11"] forState:UIControlStateNormal];
            
        } else {
            
            [proBtn setTitle:@"取消" forState:UIControlStateNormal];
            [proBtn setImage:[UIImage imageNamed:@"gou22"] forState:UIControlStateNormal];
            
        }
        
        [proBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -24, 0, 0)];
        [proBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        proBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [proBtn addTarget:self action:@selector(allChooseClick) forControlEvents:UIControlEventTouchUpInside];
        [viewAdd addSubview:proBtn];
        
        
        
        
        self.delegateBtn.backgroundColor = BenSe_COLOR;
        self.delegateBtn = [[UIButton alloc]initWithFrame:CGRectMake(ViewWidth - 60, 0, 60, 44)];
        [self.delegateBtn addTarget:self action:@selector(delete2:) forControlEvents:UIControlEventTouchUpInside];
        [viewAdd addSubview:self.delegateBtn];
        [self.delegateBtn setTitleColor:BenSe_COLOR forState:UIControlStateNormal];
        [self.delegateBtn setTitle:@"删除" forState:UIControlStateNormal];
        self.delegateBtn.titleLabel.font = [UIFont systemFontOfSize:15];
      
        return View2;
        
    }
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 0.001;

}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    
    //并且title不为“取消”的时候

    if ((self.openedMenuCell != nil)&&
        (self.openedMenuCell.isOpenMenu = YES)&&
        (self.openedMenuCellIndex.row == indexPath.row) && ![self.backBtn.currentTitle isEqualToString:@"取消"] )
    {
        return 160;
    }else
    {
        return 100;
    }

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell2";
    cell = (ProductViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    if (cell == nil)
    {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"ProductViewCell" owner:self options:nil][0];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.dataSource = self;
        
        //给btn1设置tag值
        cell.btn1.tag = indexPath.row;
//        cell.btn1.enabled = YES;
        
        //需要手动绘制下拉菜单视图，通过xib创建视图的时候cell的delegate和dataSource尚未确定
        [cell buildMenuView];
        
        
        
        
        
    }
    
    
    
    //将button的title传递到下一个页面
    NSDictionary *dict  = [self.list objectAtIndex:indexPath.row];
    [cell loadCell:dict buttonTitle:self.backBtn.currentTitle];
    

    
    
    //单选与多选的操作实现
    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnXuan1:)];
//    tap.numberOfTapsRequired = 1;
//    tap.numberOfTouchesRequired = 1;
//    
//    cell.imageGou.tag = indexPath.row;
//    [cell.imageGou addGestureRecognizer:tap];
//    cell.imageGou.userInteractionEnabled = YES;
    
    cell.buttonGou.tag = indexPath.row;
    [cell.buttonGou addTarget:self action:@selector(btnXuan1:) forControlEvents:UIControlEventTouchUpInside];
    
    
    if ([self.boolArray[indexPath.row] isEqualToString:@"YES"]) {
        
        [cell.imageGou setImage:[UIImage imageNamed:@"gou22"]];
    }
    else
    {
    
    
        [cell.imageGou setImage:[UIImage imageNamed:@"gou11"]];
    
    }
    
    
    
    

    return cell;




}





#pragma mark 单选多选button键方法的实现

-(void)btnXuan1:(UIButton *)sender
{
    [self chooseProduct:sender.tag];
}

- (void)chooseProduct:(NSInteger)row {
    
    if (isAllChoose) {
        
        isAllChoose = NO;
        [proBtn setTitle:@"全  选" forState:UIControlStateNormal];
    }
    
    NSString *str = self.boolArray[row];
    if ([str isEqualToString:@"YES"]) {
        
        [self.boolArray replaceObjectAtIndex:row withObject:@"NO"];
        NSArray *array = [NSArray arrayWithArray:self.dataArray];
        for (NSDictionary *dic in array) {
            
            if([[self.list[row] objectForKey:@"product_id"] isEqualToString:[dic objectForKey:@"product_id"]]) {
                
                [self.dataArray removeObject:dic];
            }
        }
    } else {
        
        [self.boolArray replaceObjectAtIndex:row withObject:@"YES"];
        [self.dataArray addObject:self.list[row]];
    }

    
    
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.backBtn.titleLabel.text isEqualToString:@"管理"]) {
        
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    } else
        
        [self chooseProduct:indexPath.row];
}


//- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    return YES;
//}




#pragma mark - MenutableViewCellDataSource

- (NSMutableArray *)dataSourceForMenuItem
{
    
//    NSLog(@"uiouuiou  %@", self.menuItemDataSourceArray);
    return self.menuItemDataSourceArray;
}

#pragma mark - MenuTableViewCellDelegate

- (void)didOpenMenuAtCell:(ProductViewCell *)menuTableVieCell withMoreButton:(UIButton *)moreButton
{
  
    

    NSIndexPath *openedIndexPath = [NSIndexPath indexPathForRow:moreButton.tag inSection:0];
    
    if ((self.openedMenuCell != nil)&&
        (self.openedMenuCell.isOpenMenu = YES)&&
        (self.openedMenuCellIndex.row == openedIndexPath.row))
    {
        //如果点的是同一个cell关闭下拉菜单并且不刷新新的cell
        self.openedMenuCell = nil;
        [self.tableView reloadRowsAtIndexPaths:@[self.openedMenuCellIndex] withRowAnimation:UITableViewRowAnimationFade];
        self.openedMenuCellIndex = nil;
        return;
    }
    
    //刷新新的Cell
    self.openedMenuCell = menuTableVieCell;
    self.openedMenuCellIndex = openedIndexPath;
    [self.tableView reloadRowsAtIndexPaths:@[self.openedMenuCellIndex] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:self.openedMenuCellIndex
                          atScrollPosition:UITableViewScrollPositionNone
                                  animated:YES];
    
}

- (void)menuTableViewCell:(ProductViewCell *)menuTableViewCell didSeletedMentItemAtIndex:(NSInteger)menuItemIndex
{
    //首先关闭打开的下拉菜单
    if ((self.openedMenuCell != nil)&&
        (self.openedMenuCell.isOpenMenu = YES)&&
        (self.openedMenuCellIndex.row == menuTableViewCell.btn1.tag))
    {
        //如果点的是同一个cell关闭下拉菜单并且不刷新新的cell
        self.openedMenuCell = nil;
        [self.tableView reloadRowsAtIndexPaths:@[self.openedMenuCellIndex] withRowAnimation:UITableViewRowAnimationFade];
        self.openedMenuCellIndex = nil;
    }
    
    //根据点击的下拉菜单的item触发相应的事件
    switch (menuItemIndex)
    {
        case 0:
        {
//            EditViewController *edit = [[EditViewController alloc] init];
            

            EditView2Controller *edit2 = [[EditView2Controller alloc]init];
            
            
            
            NSDictionary *dict = [self.list objectAtIndex:menuTableViewCell.btn1.tag];
            
            edit2.listDic = dict;
            
            [self.navigationController pushViewController:edit2 animated:YES];
            

        }
           break;
        case 1:
        {
  
            PreViewController *pre = [[PreViewController alloc]init];
            
            NSDictionary *dict = [self.list objectAtIndex:menuTableViewCell.btn1.tag];
            
            pre.listDic = dict;

            
            
            
            
            [self.navigationController pushViewController:pre animated:YES];
//            [self presentViewController:pre animated:YES completion:nil];

        }
            break;
        case 2:
        {
            
            
            
            
            deleteInteger = menuTableViewCell.btn1.tag;
            
            //删除行的所有事件
            deletIndexPath = [NSIndexPath indexPathForRow:deleteInteger inSection:0];
            

            productIDDef = [self.list[deleteInteger] objectForKey:@"product_id"];
            
            isRow = YES;

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  message:@"确定要删除商品吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alert.delegate = self;
            [alert show];
            
        }
            break;
    
            
        default:
            break;
    }
}





@end
